Skip to content

Method: lambda$loadAnnotationPropertyAxioms$5(NamedResource, OWLAnnotationAssertionAxiom)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.model.Assertion;
18: import cz.cvut.kbss.ontodriver.model.Axiom;
19: import cz.cvut.kbss.ontodriver.model.NamedResource;
20: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
21: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
22: import org.semanticweb.owlapi.model.*;
23: import org.semanticweb.owlapi.search.EntitySearcher;
24:
25: import java.net.URI;
26: import java.util.*;
27: import java.util.stream.Collectors;
28:
29: class ExplicitAxiomLoader implements AxiomLoader {
30:
31: private static final Assertion UNSPECIFIED_ASSERTION = Assertion.createUnspecifiedPropertyAssertion(false);
32:
33: private final OWLOntology ontology;
34: private final OWLDataFactory dataFactory;
35:
36: private final OwlapiAdapter adapter;
37: private final AxiomAdapter axiomAdapter;
38:
39: private Map<URI, Assertion> assertionMap;
40:
41: ExplicitAxiomLoader(OwlapiAdapter adapter, OntologySnapshot snapshot) {
42: this.adapter = adapter;
43: this.ontology = snapshot.getOntology();
44: this.dataFactory = snapshot.getDataFactory();
45: this.axiomAdapter = new AxiomAdapter(dataFactory);
46: }
47:
48: @Override
49: public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> assertions) {
50: this.assertionMap = new HashMap<>(assertions.size());
51: assertions.forEach(a -> assertionMap.put(a.getIdentifier(), a));
52: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
53: final Collection<Axiom<?>> axioms = new ArrayList<>();
54: if (assertions.contains(Assertion.createClassAssertion(false))) {
55: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, false));
56: }
57: axioms.addAll(loadDataPropertyAxioms(individual, subject, false));
58: axioms.addAll(loadObjectPropertyAxioms(individual, subject, false));
59: axioms.addAll(loadAnnotationPropertyAxioms(individual, subject, false));
60: return axioms;
61: }
62:
63: private Collection<Axiom<?>> loadDataPropertyAxioms(OWLNamedIndividual individual, NamedResource subject,
64: boolean loadAll) {
65: final Collection<Axiom<?>> axioms = new ArrayList<>();
66: EntitySearcher.getDataPropertyValues(individual, ontology.importsClosure()).forEach((dp, value) -> {
67: if (loadAll || shouldLoadDataPropertyValue(dp, value)) {
68: axioms.add(axiomAdapter.toAxiom(subject, dp, value));
69: }
70: });
71: return axioms;
72: }
73:
74: private boolean shouldLoadDataPropertyValue(OWLDataPropertyExpression dp, OWLLiteral value) {
75: final IRI dpIri = dp.asOWLDataProperty().getIRI();
76: final Optional<Assertion> assertion = assertionForAxiom(dpIri);
77: return assertion.isPresent() && OwlapiUtils.doesLanguageMatch(value, assertion.get());
78: }
79:
80: private Optional<Assertion> assertionForAxiom(IRI propertyIri) {
81: if (!doesPropertyExist(propertyIri)) {
82: return Optional.empty();
83: }
84: final URI dpUri = propertyIri.toURI();
85: // Note: I don't really like the fact that we are basing this on a randomly generated identifier of the unspecified
86: // property. Perhaps the strategy of using unspecified properties should be revisited.
87: return Optional.of(assertionMap.containsKey(dpUri) ? assertionMap.get(dpUri) :
88: assertionMap.get(UNSPECIFIED_ASSERTION.getIdentifier()));
89: }
90:
91: private boolean doesPropertyExist(IRI o) {
92: return assertionMap.containsKey(o.toURI()) || assertionMap.containsKey(UNSPECIFIED_ASSERTION.getIdentifier());
93: }
94:
95: private Collection<Axiom<?>> loadObjectPropertyAxioms(OWLNamedIndividual individual, NamedResource subject, boolean loadAll) {
96: final Collection<Axiom<?>> axioms = new ArrayList<>();
97: EntitySearcher.getObjectPropertyValues(individual, ontology.importsClosure())
98: .forEach((op, value) -> {
99: if (loadAll || doesPropertyExist(op.getNamedProperty().getIRI())) {
100: axioms.add(axiomAdapter.toAxiom(subject, op, value));
101: }
102: });
103: return axioms;
104: }
105:
106: private Collection<Axiom<?>> loadAnnotationPropertyAxioms(OWLNamedIndividual individual, NamedResource subject,
107: boolean loadAll) {
108: return ontology.importsClosure().flatMap(
109: onto -> EntitySearcher.getAnnotationAssertionAxioms(individual.getIRI(), onto)
110: .filter(a -> loadAll || shouldLoadAnnotationPropertyValue(a)))
111: .map(axiom -> axiomAdapter.toAxiom(subject, axiom)).collect(
112: Collectors.toSet());
113: }
114:
115:
116: private boolean shouldLoadAnnotationPropertyValue(OWLAnnotationAssertionAxiom axiom) {
117: final OWLAnnotationValue value = axiom.getValue();
118: final IRI apIri = axiom.getProperty().asOWLAnnotationProperty().getIRI();
119: final Optional<Assertion> assertion = assertionForAxiom(apIri);
120: return assertion.isPresent() && (!value.asLiteral().isPresent() ||
121: OwlapiUtils.doesLanguageMatch(value.asLiteral().get(), assertion.get()));
122: }
123:
124: @Override
125: public Collection<Axiom<?>> loadPropertyAxioms(NamedResource subject) {
126: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
127: final Collection<Axiom<?>> axioms = new ArrayList<>(loadDataPropertyAxioms(individual, subject, true));
128: axioms.addAll(loadObjectPropertyAxioms(individual, subject, true));
129: axioms.addAll(loadAnnotationPropertyAxioms(individual, subject, true));
130: return axioms;
131: }
132: }